﻿using System;
using System.Collections.Generic;

namespace Core.Experiment
{
    public class BlackBoardSystem : Component, IAwake
    {
        private readonly Dictionary<Type, IBlackBoard> dict = new();

        public void Awake() { }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            foreach (var pair in dict)
            {
                ReferencePool.Recycle(pair.Value);
            }
            dict.Clear();
        }

        public T Get<T>()
            where T : class, IBlackBoard
        {
            var t = typeof(T);
            if (dict.ContainsKey(t) == false)
            {
                dict[t] = ReferencePool.Acquire<T>();
            }
            return dict[t] as T;
        }

    }
}
